/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: avp_decoder.c
 *
 * Purpose: implementation decoder for av player.
 *
 * Developer:
 *   wen.gu , 2022-01-18
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "avp_decoder.h"
#include "avp_mutex.h"
#include "avp_log.h"

/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/
typedef struct _avp_decoder_node
{
    struct _avp_decoder_node* next;
    const avp_decoder_ops *decoder;
}avp_decoder_node_t;

static GU32 g_is_decoder_inited = 0;
static avp_mutex_t g_decoder_lock = NULL;
static avp_decoder_node_t* g_decoder_list_head = NULL;

/******************************************************************************
 **    inner FUNCTION DEFINITIONS
 ******************************************************************************/

static avp_decoder_node_t* avp_decoder_node_new(const avp_decoder_ops* decoder)
{
    avp_decoder_node_t* node = (avp_decoder_node_t*)malloc(sizeof(avp_decoder_node_t));

    if (node)
    {
        node->next = NULL;
        node->decoder = decoder; /** todo refine me?? */ 
        return node;       
    }   
    
    LOGE("malloc decoder list node failed\n");

    return NULL;
}

static avp_decoder_node_t** avp_decoder_node_tail()
{
    avp_decoder_node_t* node = g_decoder_list_head;

    while (node)
    {
        if (node->next == NULL)
        {
            return &node->next;
        }

        node = node->next;
    }

    return &g_decoder_list_head;
}

/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/

GErrc avp_decoder_initialize()
{
    if (!g_is_decoder_inited && !g_decoder_lock)
    {
        g_decoder_lock = avp_mutex_create();
        g_decoder_list_head = NULL;
        g_is_decoder_inited = 1;
        return G_OK;
    }

    return G_ErrInvalidStatus;
}

void avp_decoder_finish()
{
    if (g_is_decoder_inited && g_decoder_lock)
    {
        avp_mutex_lock(g_decoder_lock);
        avp_decoder_node_t* item = g_decoder_list_head;
        while (item)
        {
            avp_decoder_node_t* next = item->next;
            free(item);
            item = next;
        }
        avp_mutex_t temp_lock = g_decoder_lock;
        g_is_decoder_inited = 0;
        g_decoder_lock = NULL;
        g_decoder_list_head = NULL;
        avp_mutex_unlock(temp_lock);
        avp_mutex_destroy(temp_lock);
    }
}

GErrc avp_decoder_register(const avp_decoder_ops* decoder_ops)
{
    if (!decoder_ops)
    {
        return G_ErrBadParameter;
    }

    if (g_is_decoder_inited && g_decoder_lock)
    {
        avp_mutex_lock(g_decoder_lock);

        avp_decoder_node_t* node = avp_decoder_node_new(decoder_ops);

        if (!node)
        {
            LOGE("malloc decoder list node failed\n");
            avp_mutex_unlock(g_decoder_lock);
            return G_ErrInsufficientResources;
        }    

        avp_decoder_node_t** tail = avp_decoder_node_tail();
        *tail = node;

        avp_mutex_unlock(g_decoder_lock);
        return G_OK;
    }

    return G_ErrInvalidStatus;
}

const GErrc avp_decoder_find(const avp_codec_info_t* codec_info, avp_decoder_t* decoder)
{
    if (!codec_info || !decoder)
    {
        return G_ErrBadParameter;
    }

    if (g_is_decoder_inited && g_decoder_lock)
    {
        const avp_decoder_ops* decoder_ops = NULL;
        avp_mutex_lock(g_decoder_lock);
        avp_decoder_node_t* item = g_decoder_list_head;
        while (item)
        {
            if (item->decoder->initialize)
            {
                if (item->decoder->initialize(&decoder->handle, codec_info) == G_OK)
                {
                    decoder->ops = item->decoder;
                    avp_mutex_unlock(g_decoder_lock);
                    return G_OK;
                }
            }
            item = item->next;
        }

        avp_mutex_unlock(g_decoder_lock);
        return G_ErrNotFound;
    }

    return G_ErrInvalidStatus;;
}

void avp_decoder_ops_finish(const avp_decoder_t* decoder)
{
    if (decoder->ops && decoder->ops->finish)
    {
        decoder->ops->finish(decoder->handle);
    }
}

GErrc avp_decoder_ops_set_output_buffer(const avp_decoder_t* decoder, avp_frame_queue_t frame_queue)
{
    if (decoder->ops && decoder->ops->set_output_buffer)
    {
        return decoder->ops->set_output_buffer(decoder->handle, frame_queue);
    }

    return G_ErrNotImplemented;
}

GErrc avp_decoder_ops_send_packet(const avp_decoder_t* decoder, avp_packet_t* pkt)
{
    if (decoder->ops && decoder->ops->send_packet)
    {
        return decoder->ops->send_packet(decoder->handle, pkt);
    }

    return G_ErrNotImplemented;
}

GErrc avp_decoder_ops_receive_frame(const avp_decoder_t* decoder, avp_frame_t** frame)
{
    if (decoder->ops && decoder->ops->receive_frame)
    {
        return decoder->ops->receive_frame(decoder->handle, frame);
    }

    return G_ErrNotImplemented;
}

avp_codec_buf_provide_type_t avp_decoder_buffer_provide_type(const avp_decoder_t* decoder)
{
    if (decoder->ops && decoder->ops->output_buffer_provide_type)
    {
        return decoder->ops->output_buffer_provide_type(decoder->handle);
    }

    return G_ErrNotImplemented;    
}